package com.googlecode.dgwt.client.dojo.string;

import com.googlecode.dgwt.client.dojo.core.JsArray;
import com.googlecode.dgwt.client.dojo.core.JsFunction;
import com.googlecode.dgwt.client.dojo.core.JsObject;

/**
 * Native Regular Expression object
 * 
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class RegExp extends JsObject {
    
    
    protected RegExp(){
    }

    /**
     * Creates an new {@link RegExp} object.
     * 
     * @param regex
     *            regular expression
     * @return the object created
     */
    public static native RegExp create(String regex) /*-{
        return new ($wnd.RegExp)(regex);
    }-*/;

    /**
     * Creates an new {@link RegExp} object.
     * 
     * @param regex
     *            regular expression
     * @param options
     *            native options like "i" (insensitive), "g" (global), etc...
     * @return the object created
     */
    public static native RegExp create(String regex, String options) /*-{
        return new ($wnd.RegExp)(regex, options);
    }-*/;

    /**
     * Tests if the value matches the current regular expression.
     * 
     * @param value
     *            a String
     * @return True if match
     */
    public final native boolean test(String value) /*-{
        return this.test(value);
    }-*/;

    /**
     * Tests if the value matches the current regular expression and
     * computes the result of this {@link RegExp} applied to the value.
     * 
     * @param value
     *            a String
     * @return an {@link RegExpResult} object, or <code>null</code> if not matched
     */
    public final native RegExpResult exec(String value) /*-{
        // FIXME testar se todos navegadores suportam. IE e FF sim.
        //       isto é o mesmo que fazer: return value.match(this);
        return this.exec(value);
    }-*/;

    /**
     * Matches the regular expression and replace it's results by the specified
     * value.
     * 
     * @param str
     *            a String to be scanned
     * @param matchRepl
     *            the value to use when replacing
     * @return the new computed String
     */
    public final native String replace(String str, String matchRepl) /*-{
        return str.replace(this, matchRepl);
    }-*/;

    /**
     * Matches the regular expression and replace it's results using the
     * specified {@link JsFunction}
     * 
     * @param str
     *            a String to be scanned
     * @param func
     *            the function to use when replacing
     * @return the new computed String
     */
    public final native String replace(String str, JsFunction func) /*-{
        return str.replace(this, func);
    }-*/;

    /**
     * Matches the regular expression and split it's results.
     * 
     * @param value
     *            a String to be scanned
     * @return native array of String
     */
    public final native JsArray<String> split(String value) /*-{
        return value.split(this);
    }-*/;

}
